package com.truckdispatching.http.controller;

import java.util.HashMap;

import org.json.JSONObject;

import com.truckdispatching.UserManager;
import com.truckdispatching.http.util.TDHttpController;
import com.truckdispatching.util.CarTypeUtil;
import com.truckdispatching.util.ProvinceCityUtil;
import com.truckdispatching.util.TruckLengthUtil;

/**
 * 获取用户资料
 * 
 * @author harry
 * 
 */
public class GetUserInfoHttpController extends TDHttpController {

	@Override
	public String getRequestUrl() {
		return mHostUrl + "/wap/getUserInfo.do";
	}

	public void sendRequest() {
		HashMap<String, String> params = new HashMap<String, String>();
		params.put("jsessionid", UserManager.getToken());
		sendRequest(params, false);
	}

	@Override
	public void onSuccess(String response) {
		try {
			JSONObject jo = getJSONObjectFromJSON(new JSONObject(response), "data", null);
			UserManager.setName(getStringFromJSON(jo, "memberBO.USRREALNAME", ""));
			UserManager.setMobile(getStringFromJSON(jo, "memberBO.USRMOBILE", ""));
			UserManager.setMobile2(getStringFromJSON(jo, "mobile1", ""));
			UserManager.setIdCard(getStringFromJSON(jo, "memberBO.USRIDNO", ""));
			UserManager.setTruckNumber(getStringFromJSON(jo, "TKPLATENO", ""));
			UserManager.setTruckType(CarTypeUtil.getTypeName(getStringFromJSON(jo, "DCT_TT", "")));
			String length = getStringFromJSON(jo, "DCT_TKLEN", "");
			if (!length.equals("")) {
				UserManager.setTruckLength(TruckLengthUtil.getTruckLengthName(length));
			} else {
				UserManager.setTruckLength("");
			}
			UserManager.setTarget1(ProvinceCityUtil.getPcNameByCode(getStringFromJSON(jo, "TKTarget1", "")));
			UserManager.setTarget2(ProvinceCityUtil.getPcNameByCode(getStringFromJSON(jo, "TKTarget2", "")));
			UserManager.setTarget3(ProvinceCityUtil.getPcNameByCode(getStringFromJSON(jo, "TKTarget3", "")));
			callOnResponseListener(null);
		} catch (Exception e) {
			callOnResponseErrorListener(-1, "JSON数据异常");
		}
	}

	@Override
	public void onError(int errorCode, String errorMessage) {
		callOnResponseErrorListener(errorCode, errorMessage);
	}
}
